home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / glx / sharelist.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  7KB  |  276 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18. ** sharelist.c
  19. **
  20. ** Test sharing display list spaces between two contexts.  The lists contain
  21. ** both geometry and textures.
  22. **
  23. ** Simon Hui
  24. ** $Revision: 1.1 $
  25. ** $Date: 1995/01/27 18:06:01 $
  26. */
  27. #include <GL/glx.h>
  28. #include <GL/glu.h>
  29. #include <X11/keysym.h>
  30. #include <X11/extensions/sgiXnmbx.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include <string.h>
  35. #include <bstring.h>
  36. #include <math.h>
  37. #include "util.h"
  38.  
  39. Display *dpy;
  40. XVisualInfo *theVi;
  41. Colormap cmapA, cmapB;
  42. Window winA, winB;
  43.  
  44. GLXContext cxA, cxB;
  45. GLuint cubeList, checkerList, stripeList;
  46.  
  47. #define W 200
  48. #define H 200
  49. int width=W, height=H;
  50. int frames=300;
  51.  
  52. #define TITLE "Shared List Test"
  53.  
  54. static int attributes[] = {
  55.     GLX_RGBA,
  56.     GLX_RED_SIZE, 1,
  57.     GLX_GREEN_SIZE, 1,
  58.     GLX_BLUE_SIZE, 1,
  59.     GLX_DEPTH_SIZE, 1,
  60.     None,
  61. };
  62.  
  63. #define Red 0xff, 0, 0
  64. #define Blue 0, 0, 0xff
  65. #define Yellow 0xff, 0xff, 0
  66. static GLubyte checkerTexture[] = {
  67.     Blue, Yellow, Blue, Yellow,
  68.     Yellow, Blue, Yellow, Blue,
  69.     Blue, Yellow, Blue, Yellow,
  70.     Yellow, Blue, Yellow, Blue,
  71. };
  72. static GLubyte stripeTexture[] = {
  73.     Red, Red, Red, Red,
  74.     Red, Yellow, Yellow, Red,
  75.     Red, Yellow, Yellow, Red,
  76.     Red, Red, Red, Red,
  77. };
  78.  
  79. GLfloat pts[][3] = {
  80.     {  1,  1,  1 },
  81.     {  1,  1, -1 },
  82.     {  1, -1, -1 },
  83.     {  1, -1,  1 },
  84.     { -1,  1,  1 },
  85.     { -1,  1, -1 },
  86.     { -1, -1, -1 },
  87.     { -1, -1,  1 },
  88. };
  89. int pi[][4] = {
  90.     { 0, 3, 2, 1 },
  91.     { 4, 5, 6, 7 },
  92.     { 4, 7, 3, 0 },
  93.     { 1, 2, 6, 5 },
  94.     { 5, 4, 0, 1 },
  95.     { 2, 3, 7, 6 },
  96. };
  97. float nv[][3] = {
  98.     {  1,  0,  0 },
  99.     { -1,  0,  0 },
  100.     {  0,  0,  1 },
  101.     {  0,  0, -1 },
  102.     {  0,  1,  0 },
  103.     {  0, -1,  0 },
  104. };
  105.  
  106. void MakeCube(GLuint list)
  107. {
  108.     int i;
  109.  
  110.     glNewList(list, GL_COMPILE);
  111.     for (i=0; i < 6; i++) {
  112.     glNormal3fv(nv[i]);
  113.     glBegin(GL_QUADS);
  114.         glTexCoord2f(1.0, 1.0);
  115.         glVertex3fv(pts[pi[i][0]]);
  116.         glTexCoord2f(1.0, 0.0);
  117.         glVertex3fv(pts[pi[i][1]]);
  118.         glTexCoord2f(0.0, 0.0);
  119.         glVertex3fv(pts[pi[i][2]]);
  120.         glTexCoord2f(0.0, 1.0);
  121.         glVertex3fv(pts[pi[i][3]]);
  122.         glEnd();
  123.     }
  124.     glEndList();
  125. }
  126.  
  127. void InitContext(void)
  128. {
  129.     static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  130.     static float diffuse[] = {0.8, 0.8, 0.8, 0.5};
  131.     static float position[] = {0.3, 0.3, 1.0, 0.0};
  132.     static float front_mat_diffuse[] = {0.8, 0.8, 0.8, 1.0};
  133.     static float lmodel_ambient[] = {0.4, 0.0, 0.4, 1.0};
  134.  
  135.     glClearColor(0.0, 0.0, 0.0, 0.0);
  136.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  137.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  138.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  139.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  140.     glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  141.     glEnable(GL_LIGHTING);
  142.     glEnable(GL_LIGHT0);
  143.  
  144.     glPixelStoref(GL_UNPACK_ALIGNMENT, 1);
  145.     glPixelStoref(GL_PACK_ALIGNMENT, 1);
  146.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  147.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  148.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  149.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  150.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  151.     glEnable(GL_TEXTURE_2D);
  152.  
  153.     glDisable(GL_DITHER);
  154.     glEnable(GL_DEPTH_TEST);
  155.     glEnable(GL_CULL_FACE);
  156.     glShadeModel(GL_FLAT);
  157.  
  158.     glMatrixMode(GL_PROJECTION);
  159.     glLoadIdentity();
  160.     gluPerspective(70.0, 1.0, 0.5, 10.0);
  161.     glMatrixMode(GL_MODELVIEW);
  162.     glLoadIdentity();
  163.     glViewport(0, 0, width, height);
  164. }
  165.  
  166. void Init( int argc, char **argv, char *geometry )
  167. {
  168.     XVisualInfo *vi;
  169.     int size;
  170.  
  171.     winA = utilCreateWindow( width, height, 100, 100, None, attributes, "Black",
  172.                  argc, argv, geometry, &dpy, &vi, NULL, NULL );
  173.     winB = utilCreateWindow( width, height, 400, 100, None, attributes, "Black",
  174.                  argc, argv, geometry, &dpy, &vi, NULL, NULL );
  175.  
  176.     cxA = glXCreateContext(dpy, vi, NULL, GL_TRUE);
  177.     cxB = glXCreateContext(dpy, vi, cxA, GL_TRUE);
  178.  
  179.     if (!cxA || !cxB) {
  180.     fprintf(stderr, "Cannot create contexts\n");
  181.     exit(1);
  182.     }
  183.     glXMakeCurrent(dpy, winA, cxA);
  184.     InitContext();
  185.  
  186.     /* Make three lists: the cube, the checker texture, and the stripe texture*/
  187.     cubeList = glGenLists(1);
  188.     checkerList = glGenLists(1);
  189.     stripeList = glGenLists(1);
  190.     MakeCube(cubeList);
  191.     glNewList(checkerList, GL_COMPILE);
  192.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 4, 4, 0, GL_RGB, GL_UNSIGNED_BYTE,
  193.          checkerTexture);
  194.     glEndList();
  195.     glNewList(stripeList, GL_COMPILE);
  196.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 4, 4, 0, GL_RGB, GL_UNSIGNED_BYTE,
  197.          stripeTexture);
  198.     glEndList();
  199.     
  200.     glXMakeCurrent(dpy, winB, cxB);
  201.     InitContext();
  202.  
  203.     size = width * height * 3;
  204. }
  205.  
  206. static float rotAngle;
  207.  
  208. void Draw(void)
  209. {
  210.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  211.     glLoadIdentity();
  212.     gluLookAt(3, 3, 3, 0, 0, 0, 0, 1, 0);
  213.  
  214.     glRotatef(rotAngle, -1.0, 1.0, -1.0);
  215.     glCallList(cubeList);
  216.  
  217.     glFinish();
  218. }
  219.  
  220. void DoTest(void)
  221. {
  222.     int size = width * height * 3;
  223.     unsigned int chksum, test;
  224.     GLuint dlist;
  225.     int i;
  226.  
  227.     for (i=0; i < frames; i++) {
  228.     rotAngle = i/(frames-1.0) * 360.0;
  229.     /* alternate between the checker texture and the stripe texture */
  230.     dlist = (i % 2) ? checkerList : stripeList;
  231.     
  232.     glXMakeCurrent(dpy, winA, cxA);
  233.     glCallList(dlist);
  234.     Draw();
  235.     
  236.     glXMakeCurrent(dpy, winB, cxB);
  237.     glCallList(dlist);
  238.     Draw();
  239.     }
  240. }
  241.  
  242. void usage(void)
  243. {
  244.     fprintf( stderr, "usage: sharelist [-geometry] [frames]\n");
  245.     exit(1);
  246. }
  247.  
  248. int main(int argc, char **argv)
  249. {
  250.     XEvent event;
  251.     GLboolean needDisplay;
  252.     char *geometry=0;
  253.     int i;
  254.  
  255.     for (i=1; i<argc; i++) {
  256.     if (!strcmp(argv[i], "-geometry")) {
  257.         geometry = argv[i];
  258.         if (i <= argc-2) {
  259.         geometry = argv[i+1];
  260.         } else {
  261.         usage();
  262.         }
  263.     }
  264.     }
  265.     Init( argc, argv, geometry );
  266.  
  267.     glXMakeCurrent( dpy, winA, cxA );
  268.  
  269.     DoTest();
  270.  
  271.     glXMakeCurrent( dpy, None, NULL );
  272.     glXDestroyContext( dpy, cxA );
  273.     glXDestroyContext( dpy, cxB );
  274.     XCloseDisplay(dpy);
  275. }
  276.